﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using RN.ERP.Web.ModelService;
using Telerik.Web.UI;
using Fly.Common.Linq;
using System.Configuration;

namespace RN.ERP.Web.EnterpriseChange
{
    public partial class EnterpriseOrgCodeChangeList : BasePage
    {
        /// <summary>
        /// 变更类型
        /// </summary>
        public int AppIDInt
        {
            get
            {
                int appID = 0;
                string appIDStr = ConfigurationManager.AppSettings["AppId"] != null ? ConfigurationManager.AppSettings["AppId"] : "";
                int.TryParse(appIDStr, out appID);
                return appID;
            }
        }
              
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //BindFlowCode();
            }
        }

        protected void ListCorps_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            int codeType = 1400;////聊城变更组织机构代码的classid
            int count = 0;
            string corpName = txtCorpName.Text.Trim();
            string flowCode = ddlStatus.SelectedValue;
            List<OrgInfo> list = new List<OrgInfo>();

            using (EnterpriseService.EnterpriseServiceClient client = new EnterpriseService.EnterpriseServiceClient())
            {
                string flowCodesStr = client.GetCorpOrgList(out count, flowCode, corpName, ListCorps.PageSize, ListCorps.CurrentPageIndex + 1, UserInfo.CurrentArea, codeType, AppIDInt);
                var flowCodesList = JsonConvert.DeserializeObject<List<Tb_FlowCode>>(flowCodesStr);

                foreach (var flowcode in flowCodesList)
                {
                    string description = flowcode.Description;
                    OrgInfo info = JsonConvert.DeserializeObject<OrgInfo>(description);
                    info.StrFlowCode = GetFlowCodeTitle(flowcode.FlowCode);
                    list.Add(info);
                }

                ListCorps.VirtualItemCount = count;
                ListCorps.DataSource = list;
            }
        }

        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "Rebind")
            {
                ListCorps.MasterTableView.SortExpressions.Clear();
                ListCorps.MasterTableView.GroupByExpressions.Clear();
                ListCorps.Rebind();
            }

        }
        
        public string GetFlowCodeTitle(object oflowcode)
        {
            if (oflowcode == null)
            {
                return string.Empty;
            }
            string flowcode = oflowcode.ToString().Trim();
            if (flowcode.StartsWith("1"))
            {
                return "审核通过";
            }
            else if (flowcode.StartsWith("0,019,021"))
            {
                return "主管退回";
            }
            else if (flowcode.IndexOf(",022") > 0)
            {
                return "再次上报";
            }
            else if (flowcode.StartsWith("0,019"))
            {
                return "已上报";
            }
            else if (flowcode.Trim().Equals("0"))
            {
                return "未上报";
            }
            else
            {
                return string.Empty;
            }
        }

        private void BindFlowCode()
        {
            //string classid = "1400";
            //string msg = string.Empty;
            //var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(basicClient.GetAllFlowStatus(out msg, UserInfo.CurrentArea, UserInfo.RoleId, classid));
            //ddlStatus.Items.Clear();
            //foreach (var key in dic.Keys)
            //{
            //    ddlStatus.Items.Add(new ListItem(key, dic[key]));
            //}
            //ddlStatus.Items.Insert(0, new ListItem("全部", ""));
            //if (dic.ContainsKey("处理中"))
            //{
            //    ddlStatus.Items.FindByText("处理中").Selected = true;
            //}
        }
    }

    class OrgInfo
    {
        public string CorpName { get; set; }
        public string CorpUserGuid { get; set; }
        public string OrgCode { get; set; }
        public string OrgGuid { get; set; }
        public string PostDate { get; set; }
        public string StrFlowCode { get; set; }
    }
}